package cn.com.gridinfo.sooc.core.my.setting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.login.activity.LoginActivity;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 修改密码
 * Created by 刘晓晨 on 2015/12/14.
 */
public class ChangePwdActivity extends BaseSoocActivity {

    @InjectView(R.id.oldPwd)//输入旧密码
            ClearEditText oldPwd;
    @InjectView(R.id.newPwd)// 输入新密码
            ClearEditText newPwd;
    @InjectView(R.id.newPwdAgain)//确认新密码
            ClearEditText newPwdAgain;
    @InjectView(R.id.btn_send)
    Button btnSend;

    private String oldPassWord;//定义三个全局变量
    private String newPassWord;
    private String sureNewPwd;
    MyDao dao;//“我的”dao

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_change_pwd);
        dao = new MyDao(this);
        ButterKnife.inject(this);
        btnSend.setEnabled(false);
        newPwdAgain.setImeOptions(EditorInfo.IME_ACTION_DONE);
        newPwdAgain.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    if (validateNotNull()) {
                        changePwd();
                    }

                }
                return false;
            }
        });
//        btnSend.setBackgroundResource(R.color.color_d3d3);
    }

    private boolean validateNotNull() {
        //账号为空
        if (StringUtils.isNull(oldPwd.getText().toString().trim())) {
            oldPwd.requestFocus();
            return false;
        }
        //账号为空
        if (StringUtils.isNull(newPwd.getText().toString().trim())) {
            newPwd.requestFocus();
            return false;
        }
        //账号为空
        if (StringUtils.isNull(newPwdAgain.getText().toString().trim())) {
            newPwdAgain.requestFocus();
            return false;
        }
        return true;
    }


    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "修改密码";
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //返回
    @OnClick(R.id.toolbar_leftbtn)
    void onGoBack() {
        finish();

    }

    @OnTextChanged(value = {R.id.oldPwd, R.id.newPwd, R.id.newPwdAgain}, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void OnTextChanged() {
        if (StringUtils.isNull(oldPwd.getText().toString().trim()) ||
                StringUtils.isNull(newPwd.getText().toString().trim()) ||
                StringUtils.isNull(newPwdAgain.getText().toString().trim())) {
            btnSend.setEnabled(false);
        } else {
            btnSend.setEnabled(true);
        }

    }

    @OnClick(R.id.btn_send)
    void changePwd() {
        oldPassWord = oldPwd.getText().toString();//拿到旧密码
        newPassWord = newPwd.getText().toString();//第一次输入的新密码
        sureNewPwd = newPwdAgain.getText().toString();//确认新密码
        if (!oldPassWord.equals(Arad.preferences.getString(LocalMark.USER_PWD.getName()))) {
            MessageUtils.showImageShortToast(this, "原始密码输入错误");
        } else if (oldPassWord.length() < 6 || oldPassWord.length() > 20) {
            MessageUtils.showImageShortToast(this, "请输入6~20位旧密码~");
        } else if (newPassWord.length() < 6 || newPassWord.length() > 20) {
            MessageUtils.showImageShortToast(this, "请输入6~20位新密码~");
        } else if (sureNewPwd.length() < 6 || sureNewPwd.length() > 20) {
            MessageUtils.showImageShortToast(this, "请输入6~20位确认密码~");
        } else if (newPassWord.contains(" ") || sureNewPwd.contains(" ")) {
            MessageUtils.showImageShortToast(this, "用户密码不能包含空格~");
        } else if (!newPassWord.equals(sureNewPwd)) {
            MessageUtils.showImageShortToast(this, "两次新密码输入不一致~");
        } else {
            init();
        }

    }

    //请求接口
    private void init() {
        dao.changePwd(oldPassWord, sureNewPwd);
    }

    // 请求成功，清楚本地缓存，跳转登录页
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.status.equals("0")) {
                MessageUtils.showRightImageShortToast(this, "密码修改成功");
                Arad.preferences.putString(LocalMark.USER_PWD.getName().toString(), newPassWord);
                Arad.preferences.flush();
                finish();
            } else {
                MessageUtils.showImageShortToast(this, dao.statusCode);
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }
}
